﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Web.UI;
namespace OnlineControl
{
    /// <summary>
    /// JRepeater控件
    /// </summary>

    [DefaultProperty("Text"), ToolboxData("<!--Repeater Begin --><div class=\"BigDiv_rep\"> <{0}:Repeater ID=\"Rep_List\" runat=server EnableViewState=\"false\" OnPreRender=\"Rep_List_PreRender\"> <HeaderTemplate><div class=\"BigDiv_Title_rep\"> <ul> <li style=\"line-height:22px;\"> <asp:CheckBox ID=\"TopCheckBox\" runat=\"server\" Text=\"ID\" EnableViewState=\"false\"/> </li><li style=\"width:10%;\"> </li>  </ul> </div> </HeaderTemplate><ItemTemplate><div class=\"BigDiv_line_rep\"  onmouseover=\"javascript:this.style.backgroundColor='#EEF8F9'\" onmouseout=\"javascript:this.style.backgroundColor='#FFFFFF'\"><ul ><li style=\"line-height:22px;\"><asp:CheckBox ID=\"ItemCheckBox\" runat=\"server\" Text='<%# Eval(\"ID\") %>'  EnableViewState=\"false\"/></li><li style=\"width:10%;\"></li></ul></div></ItemTemplate>   <FooterTemplate><div class=\"DelAllButton\"><asp:Button ID=\"btn_DelAll\" runat=\"server\" onclick=\"btn_DelAll_Click\"  CssClass=\"btn_DelAllCss\"  Text=\"删除\"  EnableViewState=\"false\"/></div></FooterTemplate></{0}:Repeater></div><!--Repeater End -->")]
    public class Repeater : System.Web.UI.WebControls.Repeater
    {
        public bool ShowPagingWhenItemLessThanPageSize { get; set; }

        private int _recordcount = 0;
        private int _pagesize = 0;
        private string _pagelink = "";
        private int _currentpage = 1;
        private string _urlSearchPrex = null;
        /// <summary>
        /// constructure
        /// </summary>
        public Repeater()
        {
            this.ShowPagingWhenItemLessThanPageSize = true;
        }

        #region _

        [Bindable(true), Category("Data"), DefaultValue("1"), Description("totalCount")]
        public int RecordCount
        {
            get
            {
                return _recordcount;
            }

            set
            {
                _recordcount = value;
            }
        }

        [Bindable(true), Category("Data"), DefaultValue("1"), Description("pagesize")]
        public int PageSize
        {
            get
            {
                return _pagesize;
            }

            set
            {
                _pagesize = value;
            }
        }

        [Bindable(true), Category("Data"), DefaultValue(""), Description("currentLink")]
        public string PageLink
        {
            get
            {
                return _pagelink;
            }

            set
            {
                _pagelink = value;
            }
        }

        [Bindable(true), Category("Data"), DefaultValue("1"), Description("currentPageIndex")]
        public int CurrentPage
        {
            get
            {
                return _currentpage;
            }

            set
            {
                _currentpage = value;
            }
        }

        public string UrlSearchPrex
        {
            get { return _urlSearchPrex; }
            set { _urlSearchPrex = value; }
        }
        #endregion



        /// <summary> 
        /// output control to page
        /// </summary>
        /// <param name="output">  </param>
        protected override void Render(HtmlTextWriter output)
        {
            base.Render(output);

            if (!(this._recordcount <= this._pagesize && !this.ShowPagingWhenItemLessThanPageSize))
            {
                //output.WriteLine("<div class=\"PageLink\">" + Pagination(_recordcount, _pagesize, _currentpage, _pagelink, _urlSearchPrex) + "</div>");
                output.WriteLine("<div class=\"pagination\">" + Pagination(_recordcount, _pagesize, _currentpage, _pagelink, _urlSearchPrex) + "</div>");
            }
        }


        /// <summary>
        /// paging function
        /// </summary>
        /// <param name="recordcount">total count</param>
        /// <param name="pagesize"></param>
        /// <param name="currentpage"></param>
        /// <param name="url"></param>
        /// <returns></returns>
        public string Pagination(int recordcount, int pagesize, int currentpage, string url, string urlSearchPrex)
        {
            int allcurrentpage = 0;
            int next = 0;
            int pre = 0;
            int startcount = 0;
            int endcount = 0;
            string currentpagestr = "";

            if (currentpage < 1) { currentpage = 1; }
            //total page count
            if (pagesize != 0)
            {
                allcurrentpage = (recordcount / pagesize);
                allcurrentpage = ((recordcount % pagesize) != 0 ? allcurrentpage + 1 : allcurrentpage);
                allcurrentpage = (allcurrentpage == 0 ? 1 : allcurrentpage);
            }
            next = currentpage + 1;
            pre = currentpage - 1;
            startcount = (currentpage + 5) > allcurrentpage ? allcurrentpage - 9 : currentpage - 4;

            endcount = currentpage < 5 ? 10 : currentpage + 5;
            if (startcount < 1) { startcount = 1; }
            if (allcurrentpage < endcount) { endcount = allcurrentpage; }
            currentpagestr = "<font color=\"#66666\">总共:&nbsp;" + allcurrentpage + "&nbsp; 页 &nbsp;&nbsp; " + recordcount + "&nbsp;条记录 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font>";

            string checkaccountName = Page.Request.QueryString["checkaccount"];
            string checkprojectName = Page.Request.QueryString["checkproject"];
            urlSearchPrex += "&checkaccount=" + checkaccountName + "&checkproject=" + checkprojectName;

            string urlPrex = url + urlSearchPrex == null ? "?page=" : "?" + urlSearchPrex + "&page=";
            currentpagestr += currentpage > 1 ?
                "<a href=\"" + urlPrex + "1\"><span class=\"disabled\"> 第一页 </span></a>&nbsp;&nbsp;&nbsp;<a href=\"" + urlPrex + pre + "\">&nbsp;<span class=\"disabled\"> &laquo 前一页 </span></a>" : "&nbsp;<span class=\"disabled\">  第一页 </span>";

            for (int i = startcount; i <= endcount; i++)
            {
                currentpagestr += currentpage == i ? "&nbsp;<a href=\"javascript:void(0)\" class=\"number current\"><span class=\"disabled\">" + "&nbsp;" + i + "&nbsp;" + "</span></a>" : "&nbsp;<a class=\"number\" href=\"" + urlPrex + i + "\"><span class=\"ssss\">" + "&nbsp;" + i + "&nbsp;" + "</span></a>&nbsp;";
            }
            currentpagestr += currentpage != allcurrentpage ? "&nbsp;&nbsp;<a href=\"" + urlPrex + next + "\"><span class=\"disabled\"> 下一页 &raquo;</span></a>&nbsp;&nbsp;<a href=\"" + urlPrex + allcurrentpage + "\">&nbsp;&nbsp;<span class=\"disabled\"> 最后一页 </span></a>" : " &nbsp;&nbsp;<font color=\"#666666\"><span class=\"disabled\">  最后一页 </span></font>";

            //currentpagestr += currentpagestr + "</div>";
            //currentpagestr += "&nbsp;&nbsp; <input id=\"Text1\" type=\"text\" />";

            //currentpagestr += "&nbsp;&nbsp; <a href=\"javascript:CheckStatus();\">GO</a>";
            return currentpagestr;
        }

    }
}
